<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
<!--
                <el-breadcrumb-item><i class="el-icon-menu"></i> 表格</el-breadcrumb-item>
-->
                <el-breadcrumb-item>部门列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
       
        <div class="handle-box">
             <el-dialog :title="title" :visible.sync="dialogFormVisible">
             <el-form ref="form" :model="form" label-width="80px">
             <el-form-item label="上级部门">
                     <el-select v-model="form.superiorDepartmentCode" placeholder="筛选部门" class="handle-select mr10" v-bind:disabled="is_show==false">
                 <el-option
                            v-for="item in departmentList"
                            :key="item.departmentCode"
                            :label="item.name"
                            :value="item.departmentCode">
                    </el-option>
            </el-select>
                </el-form-item>
                  <el-form-item label="部门编码">
                    <el-input placeholder="上级部门编码" class="input" v-model="form.superiorDepartmentCode" :disabled="true"> </el-input>-
                    <el-input v-model="form.departmentCode" class="input" v-bind:disabled="is_show==false"></el-input>
                </el-form-item>
                 <el-form-item label="部门名称">
                    
                    <el-input maxlength="30" v-model="form.name" class="handle-input" v-bind:disabled="is_show==false"></el-input>
                </el-form-item>

                <el-form-item label="备注">
                    
                    <el-input maxlength="100"  type="textarea" :rows="2"  placeholder="请输入内容"
                               v-model="form.description" v-bind:disabled="is_show==false"> </el-input>

                </el-form-item>
              
                <el-form-item>
                    <el-button type="primary" v-if="is_show" @click="onSubmit">提交</el-button>
                    <el-button v-if="is_show" @click="cancel">取消</el-button>
                    <el-button v-if="is_show==false" @click="cancel">关闭</el-button>
                </el-form-item>
            </el-form>
            </el-dialog>
      
            <el-button type="primary" icon="edit" class="handle-del mr10" @click="save">新增</el-button>
        </div>
        <div class="handle-box">
            <el-input v-model="departmentName" icon="circle-close" :on-icon-click="handleIconClick" clearable placeholder="部门名称" class="handle-input mr10"></el-input>
            <el-button type="primary" icon="search" @click="search">搜索</el-button>
        </div>
        <el-table :data="data" border style="width: 100%" ref="multipleTable" @selection-change="handleSelectionChange">
           <!-- <el-table-column type="selection" width="55"></el-table-column>-->
           <el-table-column v-if="false"  prop="departmentId" label="departmentId"  width="150">
            </el-table-column>
            <el-table-column prop="name" label="部门名称"  style="width: 25%" >
                <template slot-scope='scope'>
                    <a style="border-bottom:solid 1px; cursor: pointer"  @click="detailed(scope.$index, scope.row)"> {{scope.row.name}}</a>
                </template>
            </el-table-column>
            <el-table-column prop="departmentCode" label="部门编码" style="width: 25%" >
            </el-table-column>

            <el-table-column prop="description" label="备注" style="width: 25%" >
            </el-table-column>
            
             <el-table-column label="操作" width="170">
                <template slot-scope="scope">
                    <el-button size="small"
                            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="small" type="danger"
                               @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                    @current-change ="handleCurrentChange"
                    @size-change="handleSizeChange"
                    layout="sizes,prev, pager, next,total"
                    :page-sizes="[10,20,50]"
                    :page-size="pageSize"
                    :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                url: './static/vuetable.json',
                tableData: [],
                curPage: 1,
                title: '',
                pageSize: 10,
                total:0,
                multipleSelection: [],
                departmentName: '',
                del_list: [],
                departmentCode:'',
                superiorDepartmentCode:'',
                departmentList:'',
                dialogFormVisible: false,
                form: {
                    departmentId: null,
                    departmentCode:'',
                    description:'',
                    id:'',
                    name: '',
                    superiorDepartmentCode: ''     
                },
                is_search: false,
                is_show:false
            }
        },
        created(){
            this.getData();
        },
        computed: {
            data(){
                const self = this;
                return self.tableData.filter(function(d){
                    
                    let is_del = false;
                    for (let i = 0; i < self.del_list.length; i++) {
                   
                        if(d.name === self.del_list[i].name){
                            is_del = true;
                            break;
                        }
                    }
                    return d;
                })
            },
            totalRecord(){
                return this;
            }
        },
        methods: {
            handleCurrentChange(val){
                this.curPage = val;
                this.getData();
            },
              handleSizeChange (val) {
                 this.pageSize = val;
                 this.getData();
           
            } ,
            getData(){
                let self = this;
                if(process.env.NODE_ENV === 'development'){
                    self.url = '/ms/table/list';
                };
               var searchCriteria = {};
               searchCriteria.name = self.departmentName;

                self.$axios.post("../adm/department/page?size="+self.pageSize+"&page="+(self.curPage-1), searchCriteria).then((res) => {
                    self.tableData = res.data;
                    self.total = res.total;
                 
                });
  
      
               
            },
            
            save() {
                let self = this;  
                self.is_show = true;
                self.title = "新增部门";
                self.dialogFormVisible = true ;
                self.form.name = ''; 
                self.form.departmentId = '';
                self.form.departmentCode = '';
                self.form.superiorDepartmentCode = '';
                self.form.description = '';
                self.$axios.get("../adm/department/list", {page:self.curPage}).then((res) => {    
                self.departmentList = res.data;
                    
                }) 

            },
            handleDelete(index, row) {
                let self = this;
                self.$axios.get('../adm/department/delete?departmentId='+row.departmentId, {page:self.curPage}).then((res) => {
                    self.getData();
                    this.$message.error('删除成功！');

                })

            },
            handleIconClick(){
                let self = this;
                this.departmentName = "";
            },
             cancel() {
                let self = this;   
                self.dialogFormVisible = false ;
                             
            },
            detailed(index, row) {

                 let self = this;
                 self.is_show = false;
                 self.dialogFormVisible = true;
                 self.title = "用户详情";
                 self.$axios.get("../adm/department/list", {page:self.curPage}).then((res) => {    
                     self.departmentList = res.data;
                    
                }) 
                 self.$axios.get('../adm/department/departmentDetails?departmentId='+row.departmentId, {page:self.curPage}).then((res) =>{
                      self.form = res.data;
                      self.superiorDepartmentCode = res.data.superiorDepartmentCode;
            
                  }) 
            },
            onSubmit() {

                var self = this;
                 
                    if(self.form.superiorDepartmentCode.length > 4){
                        this.$message.success('此部门不可再添加下级部门！');
                        return;
                    }
                 
                 if(self.form.departmentCode.length != 2){
                     this.$message.success('部门编码不正确，请输入2位！');
                     return;
                }             
                 if(!self.form.name){
                     this.$message.success('部门名称不可为空！');
                     return;
                }
                  if(!self.form.departmentCode){
                     this.$message.success('部门编码不可为空！');
                     return;
                }

                 
                self.$axios({
                    method: 'post',
                    url: '../adm/department/save',
                    handleAs:"json",
                    headers:{'Content-Type':'application/json'},
                    data: JSON.stringify(self.form)
                   
                }).then(function (res) {
                     
                     self.dialogFormVisible = false ;    
                     self.$message.success('操作成功！');
                     self.getData();    
                              
                }).catch(function (err) {     
                })
              
          },
            search(){
                
               let self = this;
               var searchCriteria = {};
               searchCriteria.name = self.departmentName;
               self.curPage = 0;
               self.$axios.post("../adm/department/page?size="+self.pageSize+"&page="+self.curPage, searchCriteria).then((res) => {
                    self.tableData = res.data;
                    self.total = res.total;
    
                });
  
               
            },
            formatter(row, column) {
                return row.name;
            },
            filterTag(value, row) {
                return row.tag === value;
            },
            handleEdit(index, row) {
                let self = this;
                self.is_show = true;
                self.title = "编辑部门";
                self.dialogFormVisible = true;
                self.$axios.get("../adm/department/list", {page:self.curPage}).then((res) => {    
                     self.departmentList = res.data;
                    
                }) 
                self.$axios.get('../adm/department/departmentDetails?departmentId='+row.departmentId, {page:self.curPage}).then((res) =>{
                      self.form = res.data;
                      self.superiorDepartmentCode = res.data.superiorDepartmentCode;
            
                  }) 
           
                
                
            },
           
            handleSelectionChange(val) {
                this.multipleSelection = val;
            }
        }
    }
</script>

<style scoped>
.handle-box{
    margin-bottom: 20px;
}
.handle-select{
    width: 180px;
}
.handle-input{
    width: 180px;
    display: inline-block;
}
.input{
    width: 87px;
    display: inline-block;
}
.input1{
    width: 120px;
    display: inline-block;
}
.input2{
    width: 120px;
    display: inline-block;
    height: 100px;
}
</style>
